<?php
namespace Home\Controller;

class PayController extends HomeController {

    public function index(){
        if (!$this->loginStatus) {
            $url = urlencode(U('Pay/index'));
            redirect(U('Login/index',array('back'=>$url)));die;
        }

        if ($this->userType==0) {
            $obj = M('User');
        }elseif($this->userType==1){
            $obj = M('Mentees');
        }elseif($this->userType==2){
            $obj = M('Mentors');
        }
        $user = $obj->where('userid='.$this->userID)->find();
        $this->assign('user',$user);

    	$goodsID = $_COOKIE['goodsID'];
    	if ($goodsID=='') {
    		$this->error('Please select the services you need to purchase!');
    	}else{
    		$goodsID = explode('|', $goodsID);
    		$map['id'] = array('in',$goodsID);
    		$list = M('Goods')->where($map)->select();
    		$total = 0;
    		foreach ($list as $key => $value) {
    			$total += $value['price'];
    		}
    		$this->assign('list',$list);
    		$this->assign('total',$total);

            $state = M('States')->select();
            $this->assign('state', $state);
    		$this->display();
    	}    	
    }

    public function confirm(){
        $parm["price"] = I('post.price');
        $parm["name"] = I('post.name');
        $parm["firstName"] = I('post.firstName');
        $parm["lastName"] = I('post.lastName');
        $parm["address"] = I('post.address');
        $parm["address2"] = I('post.address2');
        $parm["state"] = I('post.state');
        $parm["city"] = I('post.city');
        $parm["zip"] = I('post.zip');
        $parm["email"] = I('post.email');
        $parm["phone"] = I('post.phone');

        $total = 0;
        foreach ($parm["price"] as $key => $value) {
            $total += $value;
        }

        //4位随机数
        $curDateTime = date("YmdHis");
        $randNum = rand(1000, 9999);
         /* 商家的定单号 */
        $parm["order_no"] = 'MT'.$curDateTime . $randNum;

        $this->assign('parm',$parm);
        $this->assign('total',$total);
        $this->assign('memberID',$this->userID);

        $this->display();
    }

    public function save(){
        $pname = I('post.pname');
        $price = I('post.price'); 

        $obj = D('Order');
        if ($data = $obj->create()) {  
            if ($obj->add($data)) {

                //保存订单详情
                for ($i=0; $i < count($pname); $i++) {
                    $detail['pname'] = $pname[$i];
                    $detail['price'] = $price[$i];
                    $detail['order_no'] = $data['order_no'];                    
                    $detail['memberID'] = $data['memberID']; 
                    $detail['createTime'] = $data['createTime'];
                    M('OrderDetail')->add($detail);    
                }

                //删除购物车
                setcookie("cartNumber",null);
                setcookie("goodsID",null);
                $state = 'SUCCESS';
                $url = U('Pay/callback');
            } else {
                $state = 'operation failed';
            }
        } else {
            $state = $obj->getError();
        }
        echo $this->echo_json_str($state , $message , $url);
    }

    public function callback(){
        $this->display();
    }
   
}